package com.burpee.web;

import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.burpee.service.AddfileService;
import com.burpee.service.BoardService;
import com.burpee.service.CoordinateService;
import com.burpee.service.ExerciseOrderService;
import com.burpee.service.ExerciseService;
import com.burpee.service.FoodService;
import com.burpee.service.MemberService;
import com.burpee.ui.Addfile;
import com.burpee.ui.Board;
import com.burpee.ui.Exercise;
import com.burpee.ui.ExerciseOrder;
import com.burpee.ui.Food;
import com.burpee.ui.Member;


@Controller
@RequestMapping("/admin.spring")
public class AdminController {
	
	@Autowired
	MemberService memberService;
	
	@Autowired
	ExerciseService exerciseService;
	
	@Autowired
	FoodService foodService;
	
	@Autowired
	BoardService boardService;
	
	@Autowired
	CoordinateService coordinateService;
	
	@Autowired
	ExerciseOrderService exerciseOrderService;
	
	@Autowired
	AddfileService addfileService;
	
	
	
	@RequestMapping(params = "action=home")
	public ModelAndView home(HttpServletRequest request, HttpServletResponse response) {
		
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("redirect:main.spring");
		return modelAndView;
	}
	
	
	
	
	@RequestMapping(params = "action=adminMode")
	public ModelAndView adminMode(HttpServletRequest request, HttpServletResponse response) {
		
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("admin/adminMain");
		return modelAndView;
	}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////	
////////////////////////////회원 관리///////////////////////////////////////////////////////////////////////////
	@RequestMapping(params = "action=adminMemberSide")
	public ModelAndView adminMember(HttpServletRequest request, HttpServletResponse response) {
		
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("admin/adminMemberSide");
		return modelAndView;
	}
	
	@RequestMapping(params = "action=adminmemberSearch")
	public ModelAndView adminmemberSearch(HttpServletRequest request, HttpServletResponse response, Integer currentPage) {
		
		GeneratePaging generatePaging = new GeneratePaging();
		
		if(currentPage == null){
			currentPage = 1;
		}
		
		int totalCount = memberService.adminMemberCount();
		
		String page = generatePaging.adminMemberpaging(currentPage, totalCount);
		
		List<Member> memberList =  memberService.adminMemberList(currentPage);
		
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("memberList", memberList);
		modelAndView.addObject("page", page);
		
		modelAndView.setViewName("admin/adminMember");
		return modelAndView;
	}
	
	@RequestMapping(params = "action=adminMemberLevel")
	public ModelAndView adminMemberLevel(HttpServletRequest request, HttpServletResponse response, Integer currentPage) {
		
		GeneratePaging generatePaging = new GeneratePaging();
		
		if(currentPage == null){
			currentPage = 1;
		}
		
		int totalCount = memberService.adminMemberCount();
		
		String page = generatePaging.adminMemberpaging(currentPage, totalCount);
		
		List<Member> memberList =  memberService.adminMemberList(currentPage);
		
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("memberList", memberList);
		modelAndView.addObject("page", page);
		
		modelAndView.setViewName("admin/adminMemberLevel");
		return modelAndView;
	}
	
	@RequestMapping(params = "action=adminMemberFire")
	public ModelAndView adminMemberFire(HttpServletRequest request, HttpServletResponse response){
		
		String checkmemberId[] = request.getParameterValues("check");
		
		memberService.memberRemove(checkmemberId);
		
		return adminmemberSearch(request, response, 1);
		
		
	}
	
	@RequestMapping(params = "action=adminMemberFireCancel")
	public ModelAndView adminMemberFireCancel(HttpServletRequest request, HttpServletResponse response){
		
		String checkmemberId[] = request.getParameterValues("check");
		
		memberService.memberRemoveCancel(checkmemberId);
		
		return adminmemberSearch(request, response, 1);

	}
	
	
	
	////관리자페이지 회원 등급 변경 - 관리자로
	@RequestMapping(params = "action=adminAdminLevel")
	public ModelAndView adminAdminLevel(HttpServletRequest request, HttpServletResponse response){
		
		String checkmemberId[] = request.getParameterValues("adminMembercheck");
		
		memberService.adminAdminLevel(checkmemberId);
		
		return adminMemberLevel(request, response, 1);	
	}
	
	////관리자페이지 회원 등급 변경 - 트레이너로
	@RequestMapping(params = "action=adminTrainerLevel")
	public ModelAndView adminTrainerLevel(HttpServletRequest request, HttpServletResponse response){
		
		String checkmemberId[] = request.getParameterValues("adminMembercheck");
		
		memberService.adminTrainerLevel(checkmemberId);
		
		return adminMemberLevel(request, response, 1);	
	}
	
	////관리자페이지 회원 등급 변경 - 일반회원으로
	@RequestMapping(params = "action=adminGeneralMemberLevel")
	public ModelAndView adminGeneralMemberLevel(HttpServletRequest request, HttpServletResponse response){
		
		String checkmemberId[] = request.getParameterValues("adminMembercheck");
		
		memberService.adminGeneralMemberLevel(checkmemberId);
		
		return adminMemberLevel(request, response, 1);	
	}
	
	////관리자페이지 회원 등급 변경 - 신입회원으로
	@RequestMapping(params = "action=adminFreshMemberLevel")
	public ModelAndView adminFreshMemberLevel(HttpServletRequest request, HttpServletResponse response){
		
		String checkmemberId[] = request.getParameterValues("adminMembercheck");
		
		memberService.adminFreshMemberLevel(checkmemberId);
		
		return adminMemberLevel(request, response, 1);	
	}

	
///////////////////////////////////////////////////////////////////////////////////////////////////////////////	
////////////////////////////운동 관리///////////////////////////////////////////////////////////////////////////
	
	@RequestMapping(params = "action=adminExerciseSide")
	public ModelAndView adminExercise(HttpServletRequest request, HttpServletResponse response) {
		
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("admin/adminExerciseSide");
		return modelAndView;
	}
	
	@RequestMapping(params = "action=adminExerciseList")
	public ModelAndView adminExerciseList(HttpServletRequest request, HttpServletResponse response, Integer currentPage) {
		
		GeneratePaging generatePaging = new GeneratePaging();
		
		if(currentPage == null){
			currentPage = 1;
		}
		
		int totalCount = exerciseService.adminExerciseCount();
		
		String page = generatePaging.adminExercisepaging(currentPage, totalCount);
		
		
		List<Exercise> exercise = exerciseService.adminExerciseList(currentPage);
     
		
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("exercise", exercise);
		modelAndView.addObject("page", page);
		modelAndView.setViewName("admin/adminExerciseList");
		
		return modelAndView;
	}
	
	@RequestMapping(params = "action=adminExercisePreAdd")
	public ModelAndView adminExercisePreAdd(HttpServletRequest request, HttpServletResponse response) {

		ModelAndView modelAndView = new ModelAndView();
		
		modelAndView.setViewName("admin/adminExerciseAdd");
		
		return modelAndView;
	}
	
	@RequestMapping(params = "action=adminExerciseAdd")
	public ModelAndView adminExerciseAdd(HttpServletRequest request, HttpServletResponse response,Exercise exercise) {
		
		Timestamp timestamp = new Timestamp(System.currentTimeMillis());


		exercise.setUpdateDate(timestamp);
		exercise.setInputDate(timestamp);
		
		exerciseService.add(exercise);
		
		return adminExerciseList(request, response, 1);
	}
	
	
	@RequestMapping(params = "action=adminExercisePreModify")
	public ModelAndView adminExercisePreModify(HttpServletRequest request, HttpServletResponse response, Exercise exercise,ExerciseOrder exerciseOrder) {
		
		exercise = exerciseService.view(exercise.getExerciseNo());
		exerciseOrder=exerciseOrderService.view(exerciseOrder);
		
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("exercise", exercise);
		modelAndView.setViewName("admin/adminExerciseModify");
		return modelAndView;
	}
	@RequestMapping(params = "action=adminExerciseModify")
	public ModelAndView adminExerciseModify(HttpServletRequest request, HttpServletResponse response, Exercise exercise,ExerciseOrder exerciseOrder){

	   exerciseService.modify(exercise);
	   exerciseOrderService.modify(exerciseOrder);
	   
				
	   ModelAndView modelAndView = new ModelAndView();
	   modelAndView.setViewName("admin/adminExerciseList");
	   return modelAndView;
	}
	
	@RequestMapping(params = "action=adminExerciseRemove")
	public ModelAndView adminExerciseRemove(HttpServletRequest request, HttpServletResponse response){
		
		String checkExerciseNo[] = request.getParameterValues("exercisecheck");
		
		exerciseService.adminExerciseRemove(checkExerciseNo);
		
		return adminExerciseList(request, response, 1);

	}
	
	@RequestMapping(params = "action=adminExerciseRemoveCancel")
	public ModelAndView adminExerciseRemoveCancel(HttpServletRequest request, HttpServletResponse response){
		
		String checkExerciseNo[] = request.getParameterValues("exercisecheck");
		
		exerciseService.adminExerciseRemoveCancel(checkExerciseNo);
		
		return adminExerciseList(request, response, 1);

	}
	
	@RequestMapping (params = "action=adminExercisePicture")
	public ModelAndView adminExercisePicture(Addfile addfile){
		
		FileUpload fileUpload = new FileUpload();
		
		if(addfile.getMultipartFile() != null && !addfile.getMultipartFile().isEmpty()){
			
			String originalFileName = addfile.getMultipartFile().getOriginalFilename();
			String imgExt = originalFileName.substring(originalFileName.lastIndexOf(".") + 1, originalFileName.length());
			
			addfile.setAddfileName(originalFileName);
			
			Timestamp timestamp = new Timestamp(System.currentTimeMillis());
			
			addfile.setInputDate(timestamp);
			addfile.setUpdateDate(timestamp);
			
			String filePath = "C://web//upload//burpee//exercise//";
			
			addfile.setAddfileLink(filePath);
			addfile.setAddfileSize(addfile.getMultipartFile().getSize());
			addfile.setAddfileType(imgExt);
			
			String addFileRealName = System.currentTimeMillis() + "@@" + originalFileName;
			addfile.setAddfileRealName(addFileRealName);
			
			boolean result = addfileService.adminAddfileExerciseAdd(addfile);
			
			if(result){
				byte[] data = addfile.getMultipartFile().getBytes();
				fileUpload.saveFile(filePath, addFileRealName, data);
			}
		}
		
		ModelAndView modelAndView = new ModelAndView();
		
		Exercise exercise = exerciseService.view(addfile.getExerciseNo());
		modelAndView.addObject("exercise", exercise);
		
		List<Addfile> addfileList = addfileService.listAddFileByExerciseNo(addfile.getExerciseNo());
		modelAndView.addObject("exerciseFileList", addfileList);
		
		modelAndView.setViewName("admin/adminExerciseFileUpload");
		
		return modelAndView;
	}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////	
////////////////////////////음식 관리///////////////////////////////////////////////////////////////////////////	
	@RequestMapping(params = "action=adminFoodSide")
	public ModelAndView adminFood(HttpServletRequest request, HttpServletResponse response) {
		
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("admin/adminFoodSide");
		return modelAndView;
	}
	
	@RequestMapping(params = "action=adminFoodList")
	public ModelAndView adminFoodList(HttpServletRequest request, HttpServletResponse response, Integer currentPage) {
		
		GeneratePaging generatePaging = new GeneratePaging();
		
		if(currentPage == null){
			currentPage = 1;
		}
		
		int totalCount = foodService.adminfoodCount();
		
		String page = generatePaging.adminFoodpaging(currentPage, totalCount);
		
		HashMap<String, String> foodchoice = new HashMap<String, String>();
		foodchoice.put("foodIngestPurpose", null);
		foodchoice.put("foodClass", null);
		foodchoice.put("foodClassfication", null);
		
		List<Food> food = foodService.adminfoodList(currentPage);
		
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("food", food);
		modelAndView.addObject("page", page);
		modelAndView.setViewName("admin/adminFoodList");
		return modelAndView;
	}
	
	@RequestMapping(params = "action=adminFoodPreAdd")
	public ModelAndView adminFoodPreAdd(HttpServletRequest request, HttpServletResponse response) {
		
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("admin/adminFoodAdd");
		return modelAndView;

	}
	@RequestMapping(params="action=adminFoodAdd")
	public ModelAndView adminFoodAdd(HttpServletRequest request, HttpServletResponse response,Food food)
	{
		Date date = new Date(System.currentTimeMillis());
		
		
		food.setUpdateDate(date);
		food.setInputDate(date);
		foodService.add(food);
			
		return adminFoodList(request, response, 1); 
	}
	
	@RequestMapping(params = "action=adminFoodPreModify")
	public ModelAndView adminFoodPreModify(HttpServletRequest request, HttpServletResponse response, Food food ) {
		
		food = foodService.view(food.getFoodNo());
		
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("food", food);
		modelAndView.setViewName("admin/adminFoodList");
		return modelAndView;
	}
	
	@RequestMapping(params="action=adminFoodModify")
	public ModelAndView adminFoodModify(HttpServletRequest request, HttpServletResponse response,Food food)
	{
		 
		foodService.modify(food);
		   
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("admin/adminFoodList");
		return modelAndView;
	}
	
	@RequestMapping(params="action=adminFoodRemove")
	public ModelAndView adminFoodRemove(HttpServletRequest request, HttpServletResponse response)
	{
		String checkfoodNo[] = request.getParameterValues("foodcheck");
		
		foodService.adminFoodRemove(checkfoodNo);
		

		return adminFoodList(request, response, 1);
	}
	
	@RequestMapping(params="action=adminFoodRemoveCancel")
	public ModelAndView adminFoodRemoveCancel(HttpServletRequest request, HttpServletResponse response)
	{
		String checkfoodNo[] = request.getParameterValues("foodcheck");
		
		foodService.adminFoodRemoveCancel(checkfoodNo);
		

		return adminFoodList(request, response, 1);
	}
	
	
	@RequestMapping (params = "action=adminFoodPicture")
	public ModelAndView adminFoodPicture(Addfile addfile){
		
		FileUpload fileUpload = new FileUpload();
		
		if(addfile.getMultipartFile() != null && !addfile.getMultipartFile().isEmpty()){
			
			String originalFileName = addfile.getMultipartFile().getOriginalFilename();
			String imgExt = originalFileName.substring(originalFileName.lastIndexOf(".") + 1, originalFileName.length());
			
			addfile.setAddfileName(originalFileName);
			
			Timestamp timestamp = new Timestamp(System.currentTimeMillis());
			
			addfile.setInputDate(timestamp);
			addfile.setUpdateDate(timestamp);
			
			String filePath = "C://web//upload//burpee//food//";
			
			addfile.setAddfileLink(filePath);
			addfile.setAddfileSize(addfile.getMultipartFile().getSize());
			addfile.setAddfileType(imgExt);
			
			String addFileRealName = System.currentTimeMillis() + "@@" + originalFileName;
			addfile.setAddfileRealName(addFileRealName);
			
			boolean result = addfileService.adminAddfileAdd(addfile);
			
			if(result){
				byte[] data = addfile.getMultipartFile().getBytes();
				fileUpload.saveFile(filePath, addFileRealName, data);
			}
		}
		
		ModelAndView modelAndView = new ModelAndView();
		
		Food food = foodService.view(addfile.getFoodNo());
		modelAndView.addObject("food", food);
		
		List<Addfile> addfileList = addfileService.listAddFileByFoodNo(addfile.getFoodNo());
		modelAndView.addObject("foodFileList", addfileList);
		
		modelAndView.setViewName("admin/adminFoodFileUpload");
		
		return modelAndView;
	}

	
///////////////////////////////////////////////////////////////////////////////////////////////////////////////	
////////////////////////////그룹 관리///////////////////////////////////////////////////////////////////////////
	@RequestMapping(params = "action=adminGroupSide")
	public ModelAndView adminGroup(HttpServletRequest request, HttpServletResponse response) {
		
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("admin/adminGroupSide");
		return modelAndView;
	}
	
	@RequestMapping(params = "action=adminHealthcareSide")
	public ModelAndView adminHealthcare(HttpServletRequest request, HttpServletResponse response) {
		
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("admin/adminHealthcareSide");
		return modelAndView;
	}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////	
////////////////////////////게시판 관리///////////////////////////////////////////////////////////////////////////
	@RequestMapping(params = "action=adminBoardSide")
	public ModelAndView adminBoard(HttpServletRequest request, HttpServletResponse response) {

		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("admin/adminBoardSide");
		return modelAndView;
	}

	@RequestMapping(params = "action=adminBoardList")
	public ModelAndView adminBoardList(HttpServletRequest request, HttpServletResponse response) {

		List<Board> board = boardService.list();

		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("board", board);
		modelAndView.setViewName("admin/adminBoardList");
		return modelAndView;
	}

	@RequestMapping(params = "action=adminBoardPreAdd")
	public ModelAndView adminBoardPreAdd(HttpServletRequest request, HttpServletResponse response) {

		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("admin/adminBoardAdd");
		return modelAndView;
	}
	@RequestMapping(params = "action=adminBoardAdd")
	public ModelAndView adminBoardAdd(HttpServletRequest request, HttpServletResponse response, Board board) {

		boardService.add(board);

		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("admin/adminBoardAdd");
		return modelAndView;
	}

	@RequestMapping(params = "action=adminBoardPreModify")
	public ModelAndView adminBoardPreModify(HttpServletRequest request, HttpServletResponse response, Board board ) {

		board = boardService.view(board.getBoardNo());

		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("board", board);
		modelAndView.setViewName("admin/adminBoardModify");
		return modelAndView;
	}
	@RequestMapping(params = "action=adminBoardModify")
	public ModelAndView adminBoardModify(HttpServletRequest request, HttpServletResponse response, Board board){

	   boardService.modify(board);
				
				
	   ModelAndView modelAndView = new ModelAndView();
	   modelAndView.setViewName("admin/adminBoardList");
	   return modelAndView;
	}
	@RequestMapping(params = "action=adminBoardRemove")
	public ModelAndView adminBoardRemove(HttpServletRequest request, HttpServletResponse response, Board boardre){
		
		boardService.remove(boardre.getBoardNo());
		
		List<Board> board = boardService.list();

		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("board", board);
		modelAndView.setViewName("admin/adminBoardList");
		return modelAndView;
	}
	
///////////////////////////////////////////////////////////////////////////////////////////////////////////////	
////////////////////////////위치 표시 관리///////////////////////////////////////////////////////////////////////////
	
	@RequestMapping(params = "action=adminLocationSide")
	public ModelAndView adminLocation(HttpServletRequest request, HttpServletResponse response) {
		
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("admin/adminLocationSide");
		return modelAndView;
	}
	
	@RequestMapping(params = "action=adminLocationAdd")
	public ModelAndView adminLocationAdd(HttpServletRequest request, HttpServletResponse response) {
		
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("admin/adminLocationAdd");
		return modelAndView;
	}
	
	@RequestMapping(params = "action=adminLocationMap")
	public ModelAndView adminLocationMap(HttpServletRequest request, HttpServletResponse response) {
		
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("admin/adminLocationMap");
		return modelAndView;
	}
	
///////////////////////////////////////////////////////////////////////////////////////////////////////////////	
////////////////////////////게시판 관리///////////////////////////////////////////////////////////////////////////
	
	
}
